<? if ($this->auto_compatiable): ?>
    /* Exmaple of autoCompatiableValues
    $autoCompatiableValues=array(
    <?
    echo "\n";
    $f = true;

    error_log(var_export($this->columns, true));

    foreach ($this->columns as $one) {
        if ($one['type'] === 'variable') {
            if (!$f) {
                echo ",";
            }
            echo "'{$one['value']}' => ";

            if (strpos($one['bind_type'], "char") !==false) {
                echo "\"string_value\" ";
            } elseif (strpos($one['bind_type'], "tinyint(1)") !==false) {
                echo "true";
            } elseif (strpos($one['bind_type'], "int") !==false) {
                echo 1;
            } else {
                echo '"value"';
            }
            echo "   //{$one['bind_type']} \n";

            $f = false;
        }
    }
    ?>
    );
    */
<? endif ?>

public function <?= $this->function_name ?>($dbh<? if ($this->group_update): ?>,$insertValues<? elseif ($this->auto_compatiable): ?>, $autoCompatiableValues <? else: ?><?
    foreach ($this->variables as $name => $setting) {
        if (!empty($setting['func_param'])) {
            echo ', $' . $name;
        }
    }
    ?><? endif; ?>){
<?= $this->custom_header_code; ?>

$runQuery="INSERT INTO  ";



<?
foreach ($this->frome_tables as $one) {
    echo '$runQuery.="';

    echo '{' . $one['table_variable'] . '}';

    if (!empty($one['table_alias'])) {
        echo " as {$one['table_alias']}";
    }



    echo "\";\n";

    //insert only support ont table;
    break;
}
?>



$columns = "(";
$values = " values( ";


<?
if ($this->auto_compatiable) {

    echo "\$_columns=array();\n";
    echo "\$_values=array();\n";
    foreach ($this->columns as $one) {

        if ($one['type'] === 'variable') {
            echo "if (isset(\$autoCompatiableValues['{$one['value']}'])){\n";
            echo "\$_columns[]=\"`{$one['column_name']}`\";\n";
            echo "\$_values[]=\":{$one['value']}\";\n";
            echo "}\n";
        } else if ($one['type'] === 'pure') {
            echo "\$_columns[]=\"`{$one['column_name']}`\";\n";
            echo "\$_values[]=\":fix_{$one['column_name']}\";\n";
        } else if ($one['type'] === 'function') {
            echo "\$_columns[]=\"`{$one['column_name']}`\";\n";
            echo "\$_values[]=\":{$one['value']}\";\n";
        }
        echo "\n";
    }

    echo '$columns .= implode(",",$_columns);' . "\n";
    echo '$values .=  implode(",",$_values);' . "\n";
} else {
    $first = true;
    foreach ($this->columns as $one) {
        if (!$first) {
            echo '$' . "columns .=',`{$one['column_name']}`';\n";
            echo '$' . "values .=',';";
        } else {
            echo '$' . "columns .=' `{$one['column_name']}`';\n";
        }
        $first = false;




        if ($one['type'] === 'variable') {
            echo '$' . "values .=':{$one['value']}';\n";
        } else if ($one['type'] === 'pure') {
            echo '$' . "values .=':fix_{$one['column_name']}';\n";
        } else if ($one['type'] === 'function') {
            echo '$' . "values .='{$one['value']}';\n";
        }
        echo "\n";
    }
}
echo '$columns .= ")";' . "\n";
echo '$values .= ") ";' . "\n";

echo '$runQuery.=$columns.$values;';
?>

<?
if ($this->insert_update_method) {
    echo "\n";
    echo '$updateStr=\' ON DUPLICATE KEY UPDATE \';';
    echo "\n";

    $first = true;
    foreach ($this->columns as $one) {


        if (!$one['is_unique_index']) {

            if (!$first) {
                echo '$' . "updateStr .=',`{$one['column_name']}`=';";
            } else {
                echo '$' . "updateStr .=' `{$one['column_name']}`=';";
            }
            $first = false;


            echo "\n";


            if ($one['type'] === 'variable') {
                echo '$' . "updateStr .=':{$one['value']}';\n";
            } else if ($one['type'] === 'pure') {
                echo '$' . "updateStr .=':fix_{$one['column_name']}';\n";
            } else if ($one['type'] === 'function') {
                echo '$' . "updateStr .='{$one['value']}';\n";
            }




            echo "\n";
        }
    }

    echo '$runQuery.=$updateStr;';
    echo "\n";
}
?>






try {
$stmt = $dbh->prepare($runQuery);
<? if ($this->around_transaction): ?>
    $dbh->beginTransaction();
    $errorFlag = false;

<? endif; ?>

<? if ($this->group_update): ?>

    $totalCount=0;
    $insertIds=array();
    foreach ($insertValues as $one) {
    <? $this->generatPdoBinds('$stmt', true); ?>
    <? $this->generatPdoFixStrBinds('$stmt'); ?>
    $stmt_r = $stmt->execute();
    if ($stmt_r) {
    $totalCount+= $stmt->rowCount();
    $insertIds[]=$dbh->lastInsertId ();
    } else {

    $errorMessage='db error info:' . var_export($stmt->errorInfo(), true) . " query:" . $runQuery;

    <? if (!empty($this->error_log)): ?>
        <?= $this->error_log ?>($errorMessage);


    <? endif ?>
    throw new Exception($errorMessage);

    <? if ($this->around_transaction): ?>
        $errorFlag = true;

        break;

    <? endif; ?>
    }



    }
    <? if ($this->around_transaction): ?>
        if ($errorFlag) {
        $dbh->rollBack();
        $totalCount=0;
        } else {
        $dbh->commit();
        }



    <? endif; ?>

    <? if ($this->return_last_id): ?>
        return $insertIds;
    <? else: ?>
        return $totalCount;
    <? endif ?>

<? else: ?>
    <? $this->generatPdoBinds('$stmt'); ?>
    <? $this->generatPdoFixStrBinds('$stmt'); ?>


    $stmt_r = $stmt->execute();
    if($stmt_r){
    <? if ($this->return_last_id): ?>
        return $dbh->lastInsertId ();
    <? else: ?>
        return $stmt->rowCount();
    <? endif ?>

    }
    else {

    <? if (!empty($this->error_log)): ?>
        <?= $this->error_log ?>('db error info:' . var_export($stmt->errorInfo(), true)." query:".$runQuery);
    <? else: ?>
        //$errorMessage='db error info:' . var_export($stmt->errorInfo(), true)." query:".$runQuery;
    <? endif ?>
    return false;
    }
<? endif; ?>

}
catch ( PDOException $x ) {
<? if (!empty($this->error_log)): ?>
    <?= $this->error_log ?>('db error info:' . $x->getMessage()." query:".$runQuery);
<? else: ?>
    //$errorMessage='db error info:' . $x->getMessage()." query:".$runQuery;
<? endif ?>
<? if ($this->around_transaction): ?>
    $dbh->rollBack();

<? endif; ?>
throw $x;
}
<?= $this->custom_tailer_code; ?>

}
